#include "ReaderStatusProc.h"

#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <assert.h>


#define NB_BLOCS 1024*1000

void* ptrs[NB_BLOCS+1];
int size_bloc=512*1024;

void allouer(int steps){
  assert(steps < NB_BLOCS);
  for(int i=0;i<steps;i++){
    ptrs[i]=malloc(size_bloc);
    ((char*) ptrs[i])[size_bloc-1]='#';
  }
  ptrs[NB_BLOCS]=NULL;
}


void ecrire(int to,int steps){
  for(int i=0;i<to;i++){
      {
	for(char* c=(char*)ptrs[i];*c!='#';c++){
	  *c='a';
	}
      }
  }
} 

int nbblocs=10000;
#define UNIT 1
int main(int argc,char* argv[]){
  
  if(argc==3){
    size_bloc=atoi(argv[1])*UNIT;
    nbblocs=atoi(argv[2]); 
  }
  
  int to=1;
  status_t st;
  //printf("MM:%d\n",nbblocs);
  
  for(to=1;to<=nbblocs;to+=100)
    {
    pid_t child =fork();
    if(child==0)
    {
      allouer(nbblocs);   
      ecrire(to,nbblocs);
      
      ReaderStatusProc::getSelfStatus(st);
      printf("%d %d %f %d %d\n",
	     st.vmPeak/1024,
	     st.vmHWM/1024,
	     (float)((float)to/(float)nbblocs),
	     st.vmRSS/1024,st.vmData/1024);
      
      exit(0);
    }
    else
      {
	int x;
	wait(&x);
      }
  }
  return 0;
}
